package com.lht.testDemo;

import org.junit.Test;

import java.util.*;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-06-26
 */
public class ComparatorTest {


    @Test
    public void test(){

        Random random=new Random();
        List<A> aList=new ArrayList<>();
        for(int i=0;i<10;i++){
          A a=new A(random.nextInt(100),random.nextInt(100));
          aList.add(a);
        }
        A[] arr=aList.toArray(new A[aList.size()]);
        Arrays.sort(arr,new Comparator<A>(){

            @Override
            public int compare(A o1, A o2) {
                return o1.getId()-o2.getId();
            }
        });
        System.out.println(Arrays.toString(arr));

        Arrays.sort(arr,new Comparator<A>(){

            @Override
            public int compare(A o1, A o2) {
                return o2.getId()-o1.getId();
            }
        });
        System.out.println(Arrays.toString(arr));

        Arrays.sort(arr,new Comparator<A>(){

            @Override
            public int compare(A o1, A o2) {
                return o1.getNd()-o2.getNd();
            }
        });
        System.out.println(Arrays.toString(arr));
    }

}

class A{

    private  Integer id;

    private Integer nd;

    public A(Integer id, Integer nd) {
        this.id = id;
        this.nd = nd;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getNd() {
        return nd;
    }

    public void setNd(Integer nd) {
        this.nd = nd;
    }

    @Override
    public String toString() {
        return "A{" +
                "id=" + id +
                ", nd=" + nd +
                '}';
    }
}
